package com.sugon.controller.analyse;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.EnumUtil;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.dao.DeviceInfoDao;
import com.sugon.entity.ModuleArchivesEntity;
import com.sugon.modules.analyse.consts.KpiTarget;
import com.sugon.modules.analyse.consts.MoldKpiTarget;
import com.sugon.modules.analyse.model.BarChartOptionModel;
import com.sugon.modules.analyse.model.chart.KpiTargetBarEchartOptionDataVO;
import com.sugon.modules.analyse.model.vo.DeviceKpiStatParamVO;
import com.sugon.modules.analyse.model.vo.MoldKpiStatParamVO;
import com.sugon.modules.kpi.model.dto.KpiDeviceDTO;
import com.sugon.modules.kpi.service.IKpiTargetComputor;
import com.sugon.modules.kpi.service.IMoldKpiTargetComputor;
import com.sugon.modules.utils.StrUtil;
import com.sugon.service.ModuleArchivesService;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: KPI统计分析Controller
 * @author: YuXD
 * @create: 2020-11-15 09:46
 **/
@RestController
public class KpiReportController {

    @Autowired
    private List<IKpiTargetComputor> kpiTargetComputors;
    @Autowired
    private List<IMoldKpiTargetComputor> moldKpiTargetComputors;
    @Autowired
    private DeviceInfoDao deviceInfoDao;
    @Autowired
    private ModuleArchivesService moduleArchivesService;

    /**
     * 设备KPI统计
     *
     * @return
     */
    @NonBusinessParamValid
    @RequestMapping("kpi/stat")
    public R deviceKpiStat(@Validated @RequestBody DeviceKpiStatParamVO kpiStatParam, BindingResult bindingResult) {
        List<KpiTargetBarEchartOptionDataVO> kpiTargetBarEchartOptions = new ArrayList<>();
        // 1、获取当前设备组织下的设备ID列表
        List<KpiDeviceDTO> deviceIdLst = deviceInfoDao.listAll(kpiStatParam.getDeviceOrganizationId());
        if (CollectionUtil.isEmpty(deviceIdLst)) {
            return R.ok().put("kpiOptions", kpiTargetBarEchartOptions);
        }
        kpiStatParam.setDeviceIdLst(deviceIdLst);
        kpiStatParam.setBatchDeviceIdSql(StrUtil.generateBatchSql(deviceIdLst.stream().map(KpiDeviceDTO::getId).collect(Collectors.toList()), "DEV_ID", 1000));

        // 获取需要处理的KPI维度类别
        List<String> targetList = kpiStatParam.getTargetList();
        for (String target : targetList) {
            KpiTarget kpiTarget = EnumUtil.fromString(KpiTarget.class, target);
            if (kpiTarget == null) {
                continue;
            }
            KpiTargetBarEchartOptionDataVO kpiTargetBarEchartOption;
            for (IKpiTargetComputor kpiTargetComputor : kpiTargetComputors) {
                if (kpiTargetComputor.getType() == kpiTarget) {
                    kpiTargetBarEchartOption = kpiTargetComputor.compute(kpiStatParam);
                    kpiTargetBarEchartOption.setKpiTarget(kpiTarget);
                    kpiTargetBarEchartOptions.add(kpiTargetBarEchartOption);
                }
            }
        }
        return R.ok().put("kpiOptions", kpiTargetBarEchartOptions);
    }

    /**
     * 模具KPI统计
     */
    @NonBusinessParamValid
    @RequestMapping("kpi/mold/stat")
    public R moldKpiStat(@Validated @RequestBody MoldKpiStatParamVO kpiStatParam, BindingResult bindingResult) {
        List<BarChartOptionModel> kpiTargetBarEchartOptions = new ArrayList<>();
        // 校验模具是否存在
        List<String> moldCodeList = kpiStatParam.getMoldCode();
        for (String moldCode : moldCodeList) {
            ModuleArchivesEntity mold = moduleArchivesService.queryObjectByCode(moldCode);
            if (mold == null) {
                return R.error("非法模具编码[" + moldCode + "]！");
            }
            kpiStatParam.getMoldNo().add(mold.getModuleNo());
        }
        // 获取需要处理的KPI维度类别
        List<String> targetList = kpiStatParam.getTargetList();
        for (String target : targetList) {
            MoldKpiTarget kpiTarget = EnumUtil.fromString(MoldKpiTarget.class, target);
            if (kpiTarget == null) {
                continue;
            }
            BarChartOptionModel barChartOptionModel;
            for (IMoldKpiTargetComputor kpiTargetComputor : moldKpiTargetComputors) {
                if (kpiTargetComputor.getType() == kpiTarget) {
                    barChartOptionModel = kpiTargetComputor.compute(kpiStatParam);
                    kpiTargetBarEchartOptions.add(barChartOptionModel);
                }
            }
        }
        return R.ok().put("kpiOptions", kpiTargetBarEchartOptions);
    }

}
